﻿using System;
using SaleManagement.Web.App_Code;
using SaleManagement.Domains;
using SaleManagement.Services;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.HtmlControls;

namespace SaleManagement.Web.Pages
{
  public partial class CustomerInfo : BasePage
  {
    #region Properties
    private const string SEARCH_CUSTOMER_CODE = "GuaranteeList_Search_Customer_Code";
    private const string SEARCH_CUSTOMER_NAME = "GuaranteeList_Search_Customer_Name";
    private const string SEARCH_CUSTOMER_PHONE = "GuaranteeList_Search_Phone";
    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        #region Load Session

        txtSearchCode.Text = GetSessionSearch(SEARCH_CUSTOMER_CODE);
        txtSearchName.Text = GetSessionSearch(SEARCH_CUSTOMER_NAME);
        txtSearchPhone.Text = GetSessionSearch(SEARCH_CUSTOMER_PHONE);
        #endregion
        //LoadCustomerList();
      }
    }

    protected void gvCustomer_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
      LoadCustomerList();
    }

    protected void gvCustomer_PageIndexChanged(object sender, EventArgs e)
    {
      LoadCustomerList();
    }

    protected void gvPurchaseDetail_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
      LoadPurchaseDetail();
    }

    protected void gvPurchaseDetail_PageIndexChanged(object sender, EventArgs e)
    {
      LoadPurchaseDetail();
    }

    protected void gvGuarantee_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
      LoadGuarantee();
    }

    protected void gvGuarantee_PageIndexChanged(object sender, EventArgs e)
    {
      LoadGuarantee();
    }
    #endregion

    #region Methods
    private void LoadCustomerList()
    {
      string customerCode = null;
      string customerName = null;
      string phone = null;

      if (!string.IsNullOrEmpty(txtSearchCode.Text.Trim()))
      {
        customerCode = txtSearchCode.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchName.Text.Trim()))
      {
        customerName = txtSearchName.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchPhone.Text.Trim()))
      {
        phone = txtSearchPhone.Text.Trim();
      }

      ConfigSessionSearch(SEARCH_CUSTOMER_CODE, customerCode);
      ConfigSessionSearch(SEARCH_CUSTOMER_NAME, customerName);
      ConfigSessionSearch(SEARCH_CUSTOMER_PHONE, phone);

      TList<Customer> ds = new CustomerService().Search(customerCode, customerName, phone);
      gvCustomer.DataSource = ds;
      gvCustomer.DataBind();
    }

    private void ApplyMasterLayout()
    {
      gvPurchaseDetail.BeginUpdate();
      try
      {
        gvPurchaseDetail.ClearSort();
        gvPurchaseDetail.GroupBy((DevExpress.Web.ASPxGridView.GridViewColumn)gvPurchaseDetail.Columns["Id"]);
      }
      catch (Exception ex)
      {
        WriteLog("CustomerInfo.aspx::Apply Master Layout", ex);
      }
      finally
      {
        gvPurchaseDetail.EndUpdate();
      }
      gvPurchaseDetail.ExpandAll();
    }

    private void ApplyGuaranteeMasterLayout()
    {
      gvGuarantee.BeginUpdate();
      try
      {
        gvGuarantee.ClearSort();
        gvGuarantee.GroupBy((DevExpress.Web.ASPxGridView.GridViewColumn)gvGuarantee.Columns["ProductCode"]);
      }
      catch (Exception ex)
      {
        WriteLog("CustomerInfo.aspx::Apply Master Layout", ex);
      }
      finally
      {
        gvGuarantee.EndUpdate();
      }
      gvGuarantee.ExpandAll();
    }

    private void LoadPurchaseDetail()
    {
      int id = -1;
      int.TryParse(hfCustomerId.Value, out id);
      gvPurchaseDetail.DataSource = new PurchaseService().GetByCustomer(id);
      gvPurchaseDetail.DataBind();
      ApplyMasterLayout();
    }

    private void LoadGuarantee()
    {
      int id = -1;
      int.TryParse(hfCustomerId.Value, out id);
      gvGuarantee.DataSource = new GuaranteeService().GetByCustomer(id);
      gvGuarantee.DataBind();
      ApplyGuaranteeMasterLayout();
    }
    #endregion
  }
}
